package remote.controllers;

import remote.utils.Client;
import remote.activities.R;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class ConnectionController extends BaseController implements OnClickListener
{
	public ConnectionController(Activity activity)
	{
		super(activity);
	}

	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.connection_btnConnectionAuto :
				try
				{
					_client = new Client();
					
					Toast.makeText(v.getContext(), "Connected !", Toast.LENGTH_SHORT).show();
				}
				catch (Exception e)
				{
					e.printStackTrace();
					Toast.makeText(v.getContext(), "Error :(", Toast.LENGTH_SHORT).show();
				}
				
				_activity.finish();
				
				break;
			case R.id.connection_btnConnect :
		        EditText txtIPAddress = (EditText)_activity.findViewById(R.id.connection_txtIPAddress);
		        
				try
				{
					if(_client != null)
						_client.close();
					
					_client = new Client(txtIPAddress.getText().toString());
					
					if(_client.isConnected())
						Toast.makeText(v.getContext(), "Connected !", Toast.LENGTH_SHORT).show();
					else
						Toast.makeText(v.getContext(), "Error :(", Toast.LENGTH_SHORT).show();
				}
				catch (Exception e)
				{
					e.printStackTrace();
					Toast.makeText(v.getContext(), "Error :(", Toast.LENGTH_SHORT).show();
				}
				
				_activity.finish();
				
		        break;
		}
	}
}
